﻿using System.Reactive;
using System.Reactive.Concurrency;
using CommonEntity.CommonImageEntities;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using ScienceAIApp.ViewModels.SubViewModels;
using ScienceAppService.ModuleInterface;
using ScienceAppService.ModuleService;

namespace ScienceAIApp.ViewModels.AlertViewModel
{
    public class SeriesChooseViewModel : AlertBaseViewModel
    {
        private ISplitService _splitService;
        private IMarkDrawService _markDrawService;
        private IList<SplitSeriesInfo> _splitSeriesInfoList { get; set; }
        private IList<LoadSeriesDescriptionViewModel> _seriesVms { get; set; }

        [Reactive] public LoadSeriesDescriptionViewModel SeriesVm1 { get; set; }
        [Reactive] public LoadSeriesDescriptionViewModel SeriesVm2 { get; set; }
        [Reactive] public LoadSeriesDescriptionViewModel SeriesVm3 { get; set; }
        [Reactive] public LoadSeriesDescriptionViewModel SeriesVm4 { get; set; }
        [Reactive] public LoadSeriesDescriptionViewModel SeriesVm5 { get; set; }
        [Reactive] public LoadSeriesDescriptionViewModel SeriesVm6 { get; set; }
        [Reactive] public LoadSeriesDescriptionViewModel SeriesVm7 { get; set; }
        [Reactive] public int PageNum { get; set; }
        [Reactive] public int PageCount { get; set; }

        [Reactive] public bool IsCanPre { get; set; }
        [Reactive] public bool IsCanNext { get; set; }
        [Reactive] public string AlertMsg { get; set; }

        public ReactiveCommand<int, Unit> TurnPageCommand { get; set; }
        public ReactiveCommand<Unit, Unit> CancelCommand { get; set; }
        public ReactiveCommand<Unit, Unit> ConfirmCommand { get; set; }

        public SeriesChooseViewModel(LoadSeriesDescriptionViewModel vm1, LoadSeriesDescriptionViewModel vm2,
            LoadSeriesDescriptionViewModel vm3, LoadSeriesDescriptionViewModel vm4, LoadSeriesDescriptionViewModel vm5,
            LoadSeriesDescriptionViewModel vm6, LoadSeriesDescriptionViewModel vm7, ISplitService splitService, IMarkDrawService markDrawService)
        {
            _splitService = splitService;
            _markDrawService = markDrawService;

            SeriesVm1 = vm1;
            SeriesVm2 = vm2;
            SeriesVm3 = vm3;
            SeriesVm4 = vm4;
            SeriesVm5 = vm5;
            SeriesVm6 = vm6;
            SeriesVm7 = vm7;

            _seriesVms = new List<LoadSeriesDescriptionViewModel>();
            _seriesVms.Add(SeriesVm1);
            _seriesVms.Add(SeriesVm2);
            _seriesVms.Add(SeriesVm3);
            _seriesVms.Add(SeriesVm4);
            _seriesVms.Add(SeriesVm5);
            _seriesVms.Add(SeriesVm6);
            _seriesVms.Add(SeriesVm7);

            PageNum = 0;
            PageCount = 0;

            foreach (var svm in _seriesVms)
            {
                svm.ChooseSeriesInteraction.RegisterHandler(ctx =>
                {
                    ChooseSeriesInteractionHandler(ctx.Input);
                    ctx.SetOutput(Unit.Default);
                });
            }

            TurnPageCommand = ReactiveCommand.CreateFromTask<int>(async num =>
            {
                if (num < 1) return;
                SetSplitSeriesInfoList(_splitSeriesInfoList?.Skip(num * 7).ToList());
            });

            CancelCommand = ReactiveCommand.CreateFromTask(async () =>
            {
                await AppEnvironment.Instance.AlertWindow.Hide();
            });

            // 选中序列
            ConfirmCommand = ReactiveCommand.CreateFromTask(async () =>
            {
                var selectVm = _seriesVms.FirstOrDefault(svm => svm.IsChecked);

                if (selectVm == null)
                {
                    AlertMsg = "Please select the sequence first ";
                    return;
                }

                await _splitService.ChooseSeries(selectVm.CurrentSeries!.Key);
                _markDrawService!.ClearData();
                await AppEnvironment.Instance.AlertWindow.Hide();
            });

        }

        public void SetSplitSeriesInfoList(IList<SplitSeriesInfo>? list)
        {
            if(list == null) return;

            _splitSeriesInfoList = list;
            PageCount = _splitSeriesInfoList.Count / 7 + 1;
            PageNum = 1;

            var count = Math.Min(list.Count, 7);

            for (var i = 0; i < count; i++)
            {
                _seriesVms[i].SetSeries(list[i]);
            }

            for (var i = count; i < 7; i++)
            {
                _seriesVms[i].SetSeries(null);
            }

            IsCanPre = PageNum > 1;
            IsCanNext = PageCount > PageNum;
        }

        private void ChooseSeriesInteractionHandler(string key)
        {
            RxApp.MainThreadScheduler.Schedule(() =>
            {
                AlertMsg = "";
                foreach (var vm in _seriesVms)
                {
                    if (vm.IsChecked)
                    {
                        vm.IsChecked = false;
                    }
                    else
                    {
                        vm.IsChecked = vm.CurrentSeries?.Key == key;
                    }
                }

            });
        }

    }
}
